package com.zhangzeqin.code.view;

import com.zhangzeqin.code.controller.UserController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.sql.SQLException;

/**
 * 窗口界面
 * @author 张泽钦
 */
public class LoginView  extends JFrame  {
    public static void main(String[] args) {
        new LoginView();
    }

      JLabel nameLabel=new JLabel("扇贝单词",JLabel.CENTER);
      SpringLayout springLayout=new SpringLayout();
      JPanel centerPanel=new JPanel(springLayout);
      JLabel userNameLabel=new JLabel("用户名:");
      JTextField userTxt=new JTextField();
      JLabel passWordLabel=new JLabel("密码:");
      JPasswordField passwordField=new JPasswordField();
      JLabel label=new JLabel("用户权限:");
      JButton loginButton=new JButton("登录");
      JButton registerButton=new JButton("注册");
      /**
       *下拉列表
       */
      Object[] arr={"普通用户","管理员用户"};
      JComboBox jComboBox=new JComboBox(arr);
      /**
       * 选择的账号表
       */
      String table=null;
      //记录选择的序号
      int num=0;
      SystemTray systemTray;
      TrayIcon trayIcon;
      LoginHandler loginHandler;
      RegisterHandler registerHandler;
    //get&&set
    public JTextField getUserTxt() {
        return userTxt;
    }

    public void setUserTxt(JTextField userTxt) {
        this.userTxt = userTxt;
    }

    public JTextField getPasswordField() {
        return passwordField;
    }

    public void setPasswordField(JPasswordField passwordField) {
        this.passwordField = passwordField;
    }




    public LoginView(){
          super("扇贝单词项目");
          loginHandler=new LoginHandler(this);

          //获取内容面板
          Container contentPane=getContentPane();
          nameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
          nameLabel.setPreferredSize(new Dimension(0,80));
          Font  centerFont=new Font("楷体",Font.PLAIN,20);
          userNameLabel.setFont(centerFont);
          userTxt.setPreferredSize(new Dimension(200,30));
          passWordLabel.setFont(centerFont);
          passwordField.setPreferredSize(new Dimension(200,30));
          label.setFont(centerFont);
          loginButton.setFont(centerFont);
          registerButton.setFont(centerFont);
          //把组件加入面板
          centerPanel.add(userNameLabel);
          centerPanel.add(userTxt);
          centerPanel.add(label);
          centerPanel.add(passWordLabel);
          centerPanel.add(passwordField);
          centerPanel.add(jComboBox);

          //增加按钮事件
          loginButton.addActionListener(loginHandler);
          loginButton.addKeyListener(loginHandler);
          centerPanel.add(loginButton);
          registerButton.addActionListener(loginHandler);
          centerPanel.add(registerButton);

          //弹簧布局
          layout(contentPane);

          //增加系统托盘
          addTray();

          //设置loginButton为默认按钮
          getRootPane().setDefaultButton(loginButton);

          //自定义图标
          URL imgUrl=LoginView.class.getClassLoader().getResource("image.png");
          setIconImage(new ImageIcon(imgUrl).getImage());

          //窗口大小
          setSize(600,400);

          //关闭退出程序
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setResizable(false);
          //可视
          setVisible(true);

          //下滑列表监听器
        jComboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                num=jComboBox.getSelectedIndex();
                if(num==1){
                    table="admin";
                }
            }
        });
      }



    /**
     * 弹簧布局
     * @param contentPane
     */
    private void layout(Container contentPane) {
        //布局userNameLabel
        Spring childWidth=Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(userTxt)),
                Spring.constant(20));
        int offSetX=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offSetX,
                SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
        //userTxt
        springLayout.putConstraint(SpringLayout.WEST,userTxt,20,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,userTxt,0,SpringLayout.NORTH,userNameLabel);
        //passWordLabel
        springLayout.putConstraint(SpringLayout.EAST,passWordLabel,0,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,passWordLabel,20,SpringLayout.SOUTH,userNameLabel);
        //passWordField
        springLayout.putConstraint(SpringLayout.WEST,passwordField,20,SpringLayout.EAST,passWordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,passwordField,0,SpringLayout.NORTH,passWordLabel);
        //label
        springLayout.putConstraint(SpringLayout.EAST,label,0,SpringLayout.EAST,passWordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,label,20,SpringLayout.SOUTH,passWordLabel);
        //jComboBox
        springLayout.putConstraint(SpringLayout.WEST,jComboBox,20,SpringLayout.EAST,label);
        springLayout.putConstraint(SpringLayout.NORTH,jComboBox,0,SpringLayout.NORTH,label);
        //loginButton
        springLayout.putConstraint(SpringLayout.WEST,loginButton,70,SpringLayout.WEST, label);
        springLayout.putConstraint(SpringLayout.NORTH,loginButton,20,SpringLayout.SOUTH,label);
        //registerButton
        springLayout.putConstraint(SpringLayout.WEST,registerButton,50,SpringLayout.EAST,loginButton);
        springLayout.putConstraint(SpringLayout.NORTH,registerButton,0,SpringLayout.NORTH,loginButton);

        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
    }

    /**
     * 增加系统托盘
     */
    private void addTray() {
        if(SystemTray.isSupported()){
            systemTray=SystemTray.getSystemTray();
            URL imgUrl=LoginView.class.getClassLoader().getResource("tray.png");
            trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());
            //设置托盘图片大小自动缩放
            trayIcon.setImageAutoSize(true);
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
            //增加最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {

                @Override
                public void windowIconified(WindowEvent e) {
                    LoginView.this.dispose();
                }
            });
            //托盘事件监听
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                        LoginView.this.setExtendedState(JFrame.NORMAL);
                    }
                    LoginView.this.setVisible(true);
                }
            });
        }
    }

}
